package com.tianhui.controller;

import com.tianhui.domain.User;
import com.tianhui.service.Up;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Controller
public class MainController {
    @Autowired
    private Up userService;

   //添加数据

    /**
     *
     * @param user
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/save")
    public User save(User user) throws Exception {
        return userService.save(user);
    }
  // 查找全部数据

    /**
     *
     * @return  返回全部数据
     * @throws Exception
     */
    @GetMapping(value = "/find")
    public List<User> find() throws Exception {
        return userService.findAll();
    }

   //通过id查找

    /**
     *
     * @param id  获取id
     * @return  返回 用户信息
     * @throws Exception
     */
    @GetMapping(value = "/findById")
    public User  findById(Integer id) throws Exception {
        Optional<User> byId = userService.findById(id);
        return  byId.get();

    }
    //修改

    /**
     *
     * @param id  获取id
     * @param name  名字
     * @throws Exception
     */
    @GetMapping(value="/updateNameById")
    public void updateNameById(Integer id,String name) throws Exception{
        userService.updateNameById(id, name);
    }
  //根据id删除

    /**
     *
     * @param id  获取id
     * @throws Exception
     */
    @DeleteMapping(value = "/deleteById")
    public void deleteById(Integer id) throws Exception {
        userService.deleteById(id);
    }
}
